package com.joker.aigc.blog.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joker.aigc.blog.domain.dto.ArticleCommentDTO;
import com.joker.aigc.blog.domain.dto.ArticleCommentQueryDTO;
import com.joker.aigc.blog.domain.mapstruct.ArticleCommentMapstruct;
import com.joker.aigc.blog.domain.po.ArticleCommentPO;
import com.joker.aigc.blog.domain.po.ArticlePO;
import com.joker.aigc.blog.domain.vo.ArticleCommentVO;
import com.joker.aigc.blog.mapper.ArticleMapper;
import com.joker.aigc.blog.service.ArticleCommentService;
import com.joker.aigc.blog.mapper.ArticleCommentMapper;
import com.joker.aigc.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author luckincoffee
 * @description 针对表【article_comment】的数据库操作Service实现
 * @createDate 2025-01-05 17:10:38
 */
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleCommentPO> implements ArticleCommentService {


    @Resource
    private ArticleCommentMapstruct articleCommentMapstruct;


    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Page<ArticleCommentVO> getCommentsByArticleId(ArticleCommentQueryDTO articleCommentQueryDTO) {
        Assert.notNull(articleCommentQueryDTO, "参数不能为空");
        Assert.notNull(articleCommentQueryDTO.getArticleId(), "文章id不能为空");
        LambdaQueryWrapper<ArticleCommentPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(articleCommentQueryDTO.getArticleId()), ArticleCommentPO::getArticleId, articleCommentQueryDTO.getArticleId());
        // 寻找根评论
        wrapper.eq(Objects.nonNull(articleCommentQueryDTO.getRootId()), ArticleCommentPO::getRootId, -1);
        Page<ArticleCommentPO> page = new Page<>(articleCommentQueryDTO.getPageNum(), articleCommentQueryDTO.getPageSize());
        Page<ArticleCommentPO> pagePO = page(page, wrapper);
        return (Page<ArticleCommentVO>) pagePO.convert(articleCommentPO -> {
            ArticleCommentVO vo = articleCommentMapstruct.toVO(articleCommentPO);
            // 递归查询子评论
            ArticleCommentQueryDTO subCommentQueryDTO = new ArticleCommentQueryDTO();
            subCommentQueryDTO.setPageNum(1);
            subCommentQueryDTO.setPageSize(articleCommentQueryDTO.getPageSize());
            vo.setChildren(getSubCommentsByCommentId(vo.getId(), subCommentQueryDTO));
            return vo;
        });
    }

    public List<ArticleCommentVO> getSubCommentsByCommentId(Long commentId, ArticleCommentQueryDTO articleCommentQueryDTO) {
        if (Objects.isNull(commentId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ArticleCommentPO> wrapper = new LambdaQueryWrapper<>();
        // 寻找子评论
        wrapper.eq(Objects.nonNull(articleCommentQueryDTO.getArticleId()), ArticleCommentPO::getToCommentId, commentId);
        IPage<ArticleCommentPO> page = new Page<>(articleCommentQueryDTO.getPageNum(), articleCommentQueryDTO.getPageSize());
        IPage<ArticleCommentPO> pagePO = page(page, wrapper);
        // 如果没有子评论，直接返回
        if (Objects.isNull(pagePO) || pagePO.getRecords().isEmpty()) {
            return Collections.emptyList();
        }
        List<ArticleCommentVO> records = pagePO.convert(articleCommentPO -> articleCommentMapstruct.toVO(articleCommentPO)).getRecords();
        // 递归查询子评论
        for (ArticleCommentVO vo : records) {
            vo.setChildren(getSubCommentsByCommentId(vo.getId(), articleCommentQueryDTO));
        }
        return records;
    }

    @Override
    public Page<ArticleCommentVO> getSubCommentsByCommentId(ArticleCommentQueryDTO articleCommentQueryDTO) {
        Assert.notNull(articleCommentQueryDTO, "参数不能为空");
        Assert.notNull(articleCommentQueryDTO.getId(), "评论id不能为空");

        LambdaQueryWrapper<ArticleCommentPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCommentPO::getToCommentId, articleCommentQueryDTO.getId());

        IPage<ArticleCommentPO> page = new Page<>(articleCommentQueryDTO.getPageNum(), articleCommentQueryDTO.getPageSize());
        IPage<ArticleCommentPO> pagePO = page(page, wrapper);
        return (Page<ArticleCommentVO>) pagePO.convert(articleCommentPO -> {
            ArticleCommentVO vo = articleCommentMapstruct.toVO(articleCommentPO);
            // 递归查询子评论
            ArticleCommentQueryDTO subCommentQueryDTO = new ArticleCommentQueryDTO();
            subCommentQueryDTO.setPageNum(1);
            subCommentQueryDTO.setPageSize(articleCommentQueryDTO.getPageSize());
            vo.setChildren(getSubCommentsByCommentId(vo.getId(), subCommentQueryDTO));
            return vo;
        });
    }

    @Override
    public void addComment(ArticleCommentDTO articleCommentDTO) {
        Assert.notNull(articleCommentDTO, "参数不能为空");
        Assert.notNull(articleCommentDTO.getArticleId(), "文章id不能为空");
        Assert.isTrue(StringUtils.isNotBlank(articleCommentDTO.getContent()), "评论内容不能为空");
        Assert.notNull(articleCommentDTO.getFromUid(), "用户信息不能为空");
        Assert.notNull(articleCommentDTO.getToCommentId(), "评论的id不能为空");
        ArticlePO articlePO = articleMapper.selectById(articleCommentDTO.getArticleId());
        AssertUtil.isTrue(Objects.nonNull(articlePO), "文章不存在");
        articleCommentDTO.setToUid(articlePO.getCreatedBy());

        // 验证文章是否存在
        // 如果是根评论
        if (Objects.equals(articleCommentDTO.getRootId(), -1L)) {
            articleCommentDTO.setToCommentId(null);
        } else {
            // 表示是子评论
            ArticleCommentPO articleCommentPO = baseMapper.selectById(articleCommentDTO.getToCommentId());
            AssertUtil.isTrue(Objects.nonNull(articleCommentPO), "负评论不存在");
            // 验证主评论是否存在
            ArticleCommentPO articleCommentPO1 = baseMapper.selectById(articleCommentPO.getRootId());
            AssertUtil.isTrue(Objects.nonNull(articleCommentPO1), "主评论不存在");
        }
        articleCommentDTO.setLikeCount(0);
        ArticleCommentPO articleCommentPO = articleCommentMapstruct.toEntity(articleCommentDTO);
        save(articleCommentPO);
    }

    @Override
    public void deleteComment(Long commentId) {

    }
}




